package cn.com.callback;

import java.util.Map;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

/**
 *
 * @author chopper
 *
 *
 */
public class query_recorddata implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(query_recorddata.class);
    private String charset = "utf-8";
    private int timeout = 100000;
    private int enableSSLflag = 0;

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        PropertiesUtil properties = new PropertiesUtil();
        def erpservletip = properties.readValue("erpservletip"); //获取配置文件erp接口IP
        def crmservletip = properties.readValue("crmservletip");//获取配置文件CRM接口IP
        def erpsecretkey = properties.readValue("erpsecretkey");//获取配置文件erp接口加密Key
        def crmurl = crmservletip + "/ensign";
        def input, json, k;
        try {
            def map = eliteBean.chaxun_sql("select parameter_type,dk_code from custom_parameter_type where dk_system='ERP'", "dataSource");
            if (map.size() == 0) {
                return "{\"msg\": \"\",\"code\": 1}"
            };
            for (def i = 0; i < map.size(); i++) {
                def parameter_type = map[i].get("parameter_type")
                def dk_code = map[i].get("dk_code");
                input = "{\"flag\":\"CRM\",\"method\":\"query_recorddata\",\"type\":\"json\",\"charset\":\"utf-8\",\"ver\":\"1\",\"secretkey\":\"" + erpsecretkey + "\",\"content\":{\"recordtype\":" + dk_code + "},\"sign\":\"\"}";
                input = Util.doHttpPost(crmurl, input, charset, timeout, enableSSLflag);
                json = Util.doHttpPost(erpservletip, input, charset, timeout, enableSSLflag);
                log.debug("json" + json);
                JSONObject obj = JSONObject.fromObject(json);
                if (obj.get("response").has("data")) {
                    JSONArray jsonarray = obj.get("response").get("data");
                    def smap = eliteBean.chaxun_sql("select isnull(max(orderno),0) maxnum from  custom_parameter where parameter_type='" + parameter_type + "'", "dataSource");
                    int maxnum = smap[0].get("maxnum");
                    for (k = 0; k < jsonarray.size(); k++) {
                        log.debug("Array:" + jsonarray.get(k).get("vcode"));
                        log.debug("Array:" + jsonarray.get(k).get("vname"));
                        def vcode = jsonarray.get(k).get("vcode");
                        def vname = jsonarray.get(k).get("vname");
                        smap = eliteBean.chaxun_sql("select count(*) as findrow from custom_parameter where parameter_id='" + vcode + "' and parameter_type='" + parameter_type + "'", "dataSource");
                        def findrow = smap[0].get("findrow");
                        if (findrow == 0) {
                            maxnum++;
                            eliteBean.update("insert into custom_parameter(parameter_id,parameter_desc,parameter_type,orderno,userflag,parameter_guid)values('" + vcode + "','" + vname + "','" + parameter_type + "'," + maxnum + ",1,newid())", "dataSource");
                        } else {
                            eliteBean.update("update custom_parameter set parameter_desc='" + vname + "' where parameter_id='" + vcode + "' and parameter_type='" + parameter_type + "'", "dataSource");
                        }
                    }
                }
            };
            def ecservletip = properties.readValue("ecservletip"); //获取配置文件ec接口IP
            def ecsecretkey = properties.readValue("ecsecretkey");//获取配置文件ec接口加密Key
            input = "{\"flag\":\"ECCRM\",\"method\":\"ec_message_banks\",\"type\":\"json\",\"charset\":\"utf-8\",\"ver\":\"1\",\"secretkey\":\"" + ecsecretkey + "\",\"content\":{\"COMPANY_CODE\":\"CN\"},\"sign\":\"\"}";
            input = Util.doHttpPost(crmurl, input, charset, timeout, enableSSLflag);
            json = Util.doHttpPost(ecservletip, input, charset, timeout, enableSSLflag);
            log.debug("bankinfo:" + json);
            JSONObject obj = JSONObject.fromObject(json);
            eliteBean.update("truncate table c_bank", "dataSource");
            JSONArray jsonarray = obj.get("content");
            for (k = 0; k < jsonarray.size(); k++) {
                eliteBean.update("insert into c_bank(bank_id,bank_name) values('" + k + "','" + jsonarray.get(k).get("BANK_NAME") + "')", "dataSource");
            };
            return "{\"msg\": \"\",\"code\": 1}"
        } catch (Exception e) {
            log.error("", e)
            return "{\"msg\": \"\",\"code\": -1}"
        }
    }
}